package com.boatAnchorAssistant.service.listener;

import com.boatAnchorAssistant.pojo.dto.role.CreateAdminRoleDto;
import com.boatAnchorAssistant.pojo.event.department.DeleteDepartmentEvent;
import com.boatAnchorAssistant.pojo.event.tenant.CreateTenantEvent;
import com.boatAnchorAssistant.pojo.event.tenant.DeleteTenantEvent;
import com.boatAnchorAssistant.service.service.in.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Joseph.Li
 * @author joseph.li@king-v.com
 * Created in 2022-06-26 16:42
 */
@Service
public class RoleListener {


    private final RoleService roleService;

    public RoleListener(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 创建企业后，需要设置企业内部的管理员角色
     *
     * @param event
     */
    @EventListener(CreateTenantEvent.class)
    public void roleListenerCreateTenantEvent(CreateTenantEvent event) {
        CreateAdminRoleDto dto=new CreateAdminRoleDto();
        BeanUtils.copyProperties(event,dto);
        dto.setRoleId(event.getAdminRoleId());

//        roleService.createAdminRole(dto);
    }


    /**
     * 监听删除企业事件
     *
     * 删除企业下的全部角色
     * @param event
     */
    @EventListener(DeleteTenantEvent.class)
    public void roleListener(DeleteTenantEvent event) {
        List<Long> roleIdList = event.getRoleIdList();
        roleService.bathRemoveRole(roleIdList);
    }


    /**
     * 监听删除部门事件
     *
     * 删除部门以及下属部门的角色
     * @param event
     */
    @EventListener(DeleteDepartmentEvent.class)
    public void roleListener(DeleteDepartmentEvent event) {
        List<Long> departmentIds = event.getDepartmentIds();
        roleService.removeRoleByDepartmentIds(departmentIds);
    }

}
